﻿using Apps.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.DAL.HC
{
    public partial class HC_FORM_REFERENCE_VALUE_Repository : DbContext<HC_FORM_REFERENCE_VALUE>
    {
        public List<HC_FORM_REFERENCE_VALUE_VO> Get_FORM_REFERENCE_VALUE_List(string formId)
        {
            //return Db.Queryable<HC_FORM_REFERENCE_VALUE>().WhereIF(!string.IsNullOrEmpty(formId), x => x.FORMID == formId).ToList();
            return Db.Queryable<HC_FORM_REFERENCE_VALUE, HC_EXTEND_FIELD>((o, i) => o.FIELDID == i.ID)
                .Where(o => o.FORMID == formId).OrderBy((o, i) => i.SORT, OrderByType.Asc)
                .Select((o, i) => new HC_FORM_REFERENCE_VALUE_VO
                {
                    ExtendFieldID = i.ID,
                    ExtendFieldNAME = i.NAME,
                    ExtendFieldTITLE = i.TITLE,
                    CONTROL_TYPE = i.CONTROL_TYPE,
                    DEFAULT_VALUE = i.DEFAULT_VALUE,
                    IS_REQUIRED = i.IS_REQUIRED,
                    BASEVALUE = o.BASEVALUE,
                    MINVALUE = o.MINVALUE,
                    MAXVALUE = o.MAXVALUE
                }
                ).ToList();
        }

        public FORM_REFERENCE_VALUE Get_FORM_REFERENCE_VALUE(string formId, string field)
        {
            return Db.Queryable<HC_FORM_REFERENCE_VALUE, HC_EXTEND_FIELD>((o, i) => o.FIELDID == i.ID)
                .Where((o, i) => o.FORMID == formId && i.NAME.Contains(field)).OrderBy((o, i) => i.SORT, OrderByType.Asc)
                .Select((o, i) => new FORM_REFERENCE_VALUE
                {
                    ExtendFieldNAME = i.NAME,
                    BASEVALUE = o.BASEVALUE,
                    MINVALUE = o.MINVALUE,
                    MAXVALUE = o.MAXVALUE,
                    CONTROL_TYPE = i.CONTROL_TYPE
                }
                ).Single();
        }
    }
}
